/**
 * File with base validators
 */


/**
 * Base validator class
 * @type {omplusClass}
 */
Omplus.validators.Validator = Omplus.base.extend(Omplus.base.Base, {

    init:function () {
    },

    omplusType:"Validator",

    /**
     * Error that this validator returns
     */
    error_name:null,

    /**
     * Returns the error
     * @return {String}
     */
    getError:function () {
        return this.error_name;
    },

    /**
     * Main function for validating
     * @return {Boolean}
     */
    isValid:function () {
    }
});

/**
 * Validates if value is empty
 * @type {omplusClass}
 */
Omplus.validators.EmptyValidator = Omplus.base.extend(Omplus.validators.Validator, {

    omplusType:"EmptyValidator",

    error_name:"is_empty",

    isValid:function (value) {
        if (value == null) return false;
        return value.length;
    }
});

/**
 * Validates if value is a number
 * @type {omplusClass}
 */
Omplus.validators.NumberValidator = Omplus.base.extend(Omplus.validators.Validator, {
    omplusType:"NumberValidator",

    error_name:"is_not_number",

    isValid:function (value) {
        if (value == null) return true;
        if (value.length)
            return !isNaN(value);
        return true;
    }
});

/**
 * Validates if value is an email
 * @type {omplusClass}
 */
Omplus.validators.EmailValidator = Omplus.base.extend(Omplus.validators.Validator, {
    omplusType:"EmailValidator",

    error_name:"is_not_valid_email",

    isValid:function (value) {
        if (value == null) return true;
        var filter = /^([\w\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (value.length)
            return filter.test(value);
        return true;
    }
});

Omplus.validators.SynchronizeValidator = Omplus.base.extend(Omplus.validators.Validator, {
    omplusType:"SynchronizeValidator",

    error_name:"is_not_match",
    validateWith : null,

    isValid:function (value) {
        if (value == null) return true;
        var helper = Omplus.globals.getObject(this.validateWith);
        if (!helper) return true;
        return value == helper.getValue();
    }
});

Omplus.validators.MinimumLengthValidator = Omplus.base.extend(Omplus.validators.Validator, {
    omplusType:"MinimumLengthValidator",

    error_name:"is_minimum",
    minimum : null,

    isValid:function (value) {
        if (value == null) return true;
        return value.length >= this.minimum;
    }
});

Omplus.validators.MaximumLengthValidator = Omplus.base.extend(Omplus.validators.Validator, {
    omplusType:"MaximumLengthValidator",

    error_name:"is_maximum",
    maximum : null,

    isValid:function (value) {
        if (value == null) return true;
        return value.length <= this.maximum;
    }
});